package com.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.user.mapper.PositionMapper;
import com.user.mapper.UserMapper;
import com.user.pojo.Position;
import com.user.pojo.User;
import com.user.pojo.data;
import com.user.reuslt.Result;
import com.user.service.UserService;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.time.Instant.now;


@Component
@org.springframework.boot.test.context.SpringBootTest
public class test
{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private UserService userService;

    @Test
    public void testSelect()
    {
        System.out.println(("----- selectAll method com.user.test ------"));

        List<Position> positionList = positionMapper.selectList(null);
        positionList.forEach(System.out::println);
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);

    }

    @Test
    public void test2()
    {
        User user = new User();
        user.setId(9);
        user.setUsername("e");
        user.setPassword("e");
        user.setName("e");
        user.setCreatedTime(now().toString());
        user.setCreatedBy("admin");
        user.setPositionId(2);

        userService.saveOrUpdate(user);
    }

    @Test
    public void test3()
    {
        User user = new User();
        user.setName("我是f");
        userMapper.update(user, new UpdateWrapper<User>().eq("userName", "f"));
    }

    @Test
    public void test4()
    {
        Map param = new HashMap();
        param.put("Name", "f");
        userMapper.deleteByMap(param);
    }

    @Test
    public void test5()
    {
        data data = new data();
        data.setUsername("f");

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<User>().eq(User::getUsername, data.getUsername());
        List<User> data1 = userMapper.selectList(userWrapper);
        if (data1.isEmpty()) {
            System.out.println("没有找到该用户的职位信息");

        }
        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().eq(Position::getId, data1.get(0).getPositionId());
        List<Position> data2 = positionMapper.selectList(positionWrapper);
        if (data2.isEmpty()) {
            System.out.println("没有找到该用户的职位信息");

        }
        List<Object> Data0 = new ArrayList<>();
        Data0.addAll(data1);
        Data0.addAll(data2);
        System.out.println(Data0);
    }
    @Test
    public void UpdateService()
    {
        data data = new data();
        data.setUsername("f");
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getUsername,data.getUsername());
        List<User> data1 = userMapper.selectList(userWrapper);
        userMapper.deleteBatchIds(data1);

    }
    @Test
    public void test6()
    {

    }



}



